package br.com.wepa.webapps.orca.controle.actions.orcamento;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import br.com.wepa.webapps.logger.TraceLogger;
import br.com.wepa.webapps.orca.controle.actions.struts.GenericAction;
import br.com.wepa.webapps.orca.logica.negocio.facade.FacadeDelegator;
import br.com.wepa.webapps.orca.logica.negocio.facade.OrcamentoFacadeHome;
import br.com.wepa.webapps.orca.logica.negocio.facade.basic.GenericFacadeHome;
import br.com.wepa.webapps.orca.logica.negocio.facade.basic.GenericTO;
import br.com.wepa.webapps.orca.logica.negocio.facade.to.OrcamentoTO;

public class OrcamentoAction extends GenericAction {
	/**
	 * Objeto de Log
	 */
	private static TraceLogger logger = new TraceLogger(OrcamentoAction.class);
	
	public static final String FORWARD_GO_CADASTRARPRODUTO = "goCadastrarProdutoEmOrc";
	public static final String FORWARD_PAGE_CADASTRARPRODUTO = "cadastroProduto";
	public static final String FORWARD_PAGE_DETALHA_PRODUTO = "detalhaProduto";
	public static final String MSG_02_01= "msg.02.01";
	public static final String MSG_02_02= "msg.02.02";
	public static final String MSG_02_03= "msg.02.03";

	
	
	private OrcamentoFacadeHome facade = null;

	public OrcamentoAction() {
		super();
	}

	@Override
	protected GenericFacadeHome getFacade() {
		return facade();
	}
	
	
	
	private OrcamentoFacadeHome facade() {
		if(facade == null){
			facade =  FacadeDelegator.getOrcamentoFacade();
		}
		return facade;
	}
	
	

	@SuppressWarnings("unchecked")
	public ActionForward goPesquisar(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		logger.entering();
		OrcamentoTO to = (OrcamentoTO) getGenericTansferObjectInRequest(request);
		super.copyFormToObject(to, form);
		to.setUpdate(false);		
		to = facade().goPesquisar(to);

		super.clearAndSetTansferObject(request, to, false);		
		
		logger.exiting();
		return mapping.findForward(FORWARD_PAGE_PESQUISAR);
	}
	
	@SuppressWarnings("unchecked")
	public ActionForward pesquisar(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		logger.entering();		
		GenericTO to = getGenericTansferObjectInRequest(request);
		super.copyFormToObject(to, form);
		to.setPagingBean(super.configPaging(request, form, mapping));	
		
		
		to = getFacade().find(to);
	
		super.clearAndSetTansferObject(request, to, false);
		super.pushActionEvent(request, form,mapping);

		logger.exiting();
		return goPesquisar(mapping, form, request, response);
	}

	public ActionForward consultarProduto(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		logger.entering();
		OrcamentoTO to = (OrcamentoTO) getGenericTansferObjectInRequest(request);
		super.copyFormToObject(to, form);
		to = facade().findProduto(to);
		super.clearAndSetTansferObject(request, to, false);
		super.pushActionEvent(request, form, mapping);
		logger.exiting();
		return goCadastrarProdutoEmOrc(mapping, form, request, response);
	}
	
	public ActionForward goCadastrarProdutoEmOrc(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {				
		logger.entering();
		super.saveToken(request);
		OrcamentoTO to = (OrcamentoTO)getGenericTansferObjectInRequest(request);
		super.copyFormToObject(to, form);
		
		to = facade().goCadastrarProdutoEmOrc(to);
		
		super.clearAndSetTansferObject(request, to, true);
		
		form.reset(mapping, request);
		logger.exiting();
		return mapping.findForward(FORWARD_PAGE_CADASTRARPRODUTO);
	}
	
		
	public ActionForward adicionarProdutoEmOrc(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		logger.entering();
		OrcamentoTO to = (OrcamentoTO)getGenericTansferObjectInRequest(request);
		super.copyFormToObject(to, form);				
		to = facade().adicionarProdutosEmOrc(to);	
		form.reset(mapping, request);
		super.setMensagens(request, MSG_02_01);
		logger.exiting();
		return super.back(mapping, form, request, response);
	}
	
	public ActionForward atualizarProdutoEmOrc(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		logger.entering();
		OrcamentoTO to = (OrcamentoTO)getGenericTansferObjectInRequest(request);
		super.copyFormToObject(to, form);				
		to = facade().atualizarProdutosEmOrc(to);	
		form.reset(mapping, request);
		super.setMensagens(request, MSG_02_03);
		logger.exiting();
		return goDetalhar(mapping, form, request, response);
	}
	
	
	public ActionForward removerProdutosEmOrc(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		logger.entering();
		OrcamentoTO to = (OrcamentoTO)getGenericTansferObjectInRequest(request);
		super.copyFormToObject(to, form);				
		to = facade().removerProdutosEmOrc(to);			
		super.setMensagens(request, MSG_02_02);
		logger.exiting();
		return goDetalhar(mapping, form, request, response);	
	}
	
	public ActionForward detalharProduto(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		logger.entering();
		OrcamentoTO to = (OrcamentoTO)getGenericTansferObjectInRequest(request);
		super.copyFormToObject(to, form);		
		to = facade.detalhaEspecificacaoProduto(to);		
		super.clearAndSetTansferObject(request, to, false);
		logger.exiting();
		return mapping.findForward(FORWARD_PAGE_DETALHA_PRODUTO);
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see br.com.wepa.webapps.orca.controle.actions.struts.GenericActionIF#goDetalhar(org.apache.struts.action.ActionMapping,
	 *      org.apache.struts.action.ActionForm,
	 *      javax.servlet.http.HttpServletRequest,
	 *      javax.servlet.http.HttpServletResponse)
	 */
	@SuppressWarnings("unchecked")
	public ActionForward goDetalhar(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		logger.entering();
		super.saveToken(request);
		OrcamentoTO to = (OrcamentoTO)getGenericTansferObjectInRequest(request);
		super.copyFormToObject(to, form);
		
		to = facade().detalhaOrcamento(to);		
		
		super.clearAndSetTansferObject(request, to, false);	
		super. pushActionEvent(request,form,mapping, mapping.findForward(FORWARD_GO_DETALHAR));
		logger.exiting();
		return mapping.findForward(FORWARD_PAGE_DETALHA);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public ActionForward remover(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		logger.entering();

		ActionForward forward = super.remover(mapping, form, request, response);
		
		if(forward.getPath().indexOf(FORWARD_GO_DETALHAR) != -1){
			forward =mapping.findForward(GLOBAL_FWPAGE_exibeMsg);
		}
		logger.exiting();
		return forward;
	}
	
	@Override
	public ActionForward gravar(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {		
		logger.entering();		
		super.checkToken(request);
	
		OrcamentoTO to = facade.instanceTO();
		super.copyFormToObject(to, form);
		
		to.setUsuario(super.getUsuarioSessao().getUsuario());
		
		OrcamentoTO updateTO = (OrcamentoTO) super.getTansferObjectInSession(request);		
		if (updateTO != null && updateTO.getUpdate()) {			
			updateTO.setStoreTO(to);			
			to = updateTO;
		}

		to = facade().persist(to);

		super.copyObjectToForm(form,to);
		super.clearTansferObject(request);
		super.setTansferObject(request,to,false);		
		
		super.setMensagens(request, APP_MSG_SUCESS);	
		
		if(to.getUpdate()){
			logger.exiting();
			return goDetalhar(mapping, form, request, response);
		}else{			
			logger.exiting();
			return goCadastrarProdutoEmOrc(mapping, form, request, response);
		}
	}
	
	
	@SuppressWarnings("unchecked")
	public ActionForward listaSubGrupos(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {		
		logger.entering();
		super.saveToken(request);
		OrcamentoTO to = (OrcamentoTO)getGenericTansferObjectInRequest(request);
		super.copyFormToObject(to, form);		
		to = facade().listaSubGrupos(to);		
		super.setTansferObject(request, to, false);		
		super.pushActionEvent(request, form,mapping);
		logger.exiting();
		return mapping.findForward(FORWARD_PAGE_CADASTRARPRODUTO);
		
	}
}